package com.htime.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by HTime on 2017/3/20.
 */
@RestController
public class CookieController extends HttpServlet {

    @RequestMapping(value = "/cookie", method = RequestMethod.GET)
    public String getCookie(HttpServletRequest req, HttpServletResponse resp) {

        // 获取客户端Cookie
        Cookie[] cookies = req.getCookies();
        if(null != cookies) {
            for (Cookie cookie : cookies)
                System.out.println(cookie.getName() + "-" + cookie.getValue());
        }

        // 创建Cookie
        Cookie cookie = new Cookie("CookieName", "Htime");
        cookie.setMaxAge(10);
        cookie.setHttpOnly(true);
        resp.addCookie(cookie);

        return "Hello cookie!";
    }

}
